package edu.nf.waimai.dao.rider.impl;
import edu.nf.util.DBUtils;
import edu.nf.waimai.dao.rider.RiderDAO;
import edu.nf.waimai.model.order.Order;
import edu.nf.waimai.model.order.OrderDTO;
import edu.nf.waimai.model.rider.Rider;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanHandler;
import org.nf.db.util.result.BeanListHandler;

import java.util.List;

/**
 * @Date 2023-06-20
 * @Author qiu
 * 骑手接口实现类
 */
public class RiderDAOImpl implements RiderDAO {
    /**
     * 骑手登录
     * @param riderName 骑手姓名
     * @return
     */
    @Override
    public Rider loginRider(String riderName) {

        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());

        String sql = "select rider_name,rider_phone,rider_type from rider where  rider_phone = ?";

        BeanHandler<Rider> riderBeanHandler = new BeanHandler<>(Rider.class);

        return sqlExecutor.executeQuery(sql,riderBeanHandler,riderName);
    }

    /**
     * 骑手注册
     * @param riderName
     * @param riderPhone
     * @param riderType
     * @return
     */
    @Override
    public Object registerRider(String riderName, String riderPhone, String riderType) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "insert into rider(rider_name,rider_phone,rider_type) values(?,?,?)";
        return sqlExecutor.insert(sql,riderName,riderPhone,riderType);
    }

    @Override
    public Rider equeryRider(String riderPhone) {

        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "select rider_phone from rider where rider_phone = ?";

        BeanHandler<Rider> riderBeanHandler = new BeanHandler<>(Rider.class);
        return sqlExecutor.executeQuery(sql,riderBeanHandler,riderPhone);
    }

    /**
     * 通过订单id修改状态
     * @param orderId
     * @param state
     * @return
     */
    @Override
    public int updateRiderOrder(String orderId, int state,int riderId) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "update orders set order_status = ?,rider_id = ? where order_id = ?";
        return sqlExecutor.executeUpdate(sql,state,riderId,orderId);
    }

    @Override
    public List<Order> listOrdersByIdAndState(int riderId, int orderState) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "select order_id,u_id,shop_id,rider_id,address_id,order_status,total_price,order_type,order_datetime,order_remarks,order_details from orders\n" +
                " where rider_id = ? and order_status = ?";
        BeanListHandler<Order> orderBeanListHandler = new BeanListHandler<>(Order.class);
        return sqlExecutor.executeQuery(sql,orderBeanListHandler,riderId,orderState);
    }

    /**
     * 查询订单
     * @param orderType 订单类型
     * @param oderState 订单状态
     * @return
     */
    @Override
    public List<OrderDTO> listOrdersByTypeAndState(String orderType, int oderState) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "SELECT \n" +
                "    o.order_id,\n" +
                "    o.u_id AS order_user_id,\n" +
                "    o.shop_id AS order_shop_id,\n" +
                "    o.rider_id AS order_rider_id,\n" +
                "    o.address_id AS order_address_id,\n" +
                "    o.order_status,\n" +
                "    o.total_price,\n" +
                "    o.order_type,\n" +
                "    o.order_datetime,\n" +
                "    o.order_remarks,\n" +
                "    o.order_details,\n" +
                "    s.shop_id AS shop_id,\n" +
                "    s.shop_img,\n" +
                "    s.shop_types_id,\n" +
                "    s.u_id AS shop_user_id,\n" +
                "    s.shop_name,\n" +
                "    s.shop_phone,\n" +
                "    s.shop_address,\n" +
                "    s.shop_datetime AS shop_create_time,\n" +
                "    s.shop_details,\n" +
                "    u.u_id AS user_id,\n" +
                "    u.u_name AS username,\n" +
                "    u.u_pwd AS password,\n" +
                "    u.u_nickname AS nickname,\n" +
                "    u.u_phone AS user_phone,\n" +
                "    u.u_avatar AS user_avatar,\n" +
                "    u.u_datetime AS user_create_time,\n" +
                "\t\t\n" +
                "    a.address_id AS address_id,\n" +
                "    a.u_id AS address_user_id,\n" +
                "    a.address_name AS recipient_name,\n" +
                "    a.address_phone AS recipient_phone,\n" +
                "    a.province,\n" +
                "    a.city,\n" +
                "    a.district,\n" +
                "    a.detail AS address_detail,\n" +
                "    a.areaCode,\n" +
                "    a.is_default AS default_address,\n" +
                "    a.address_datetime AS address_create_time\n" +
                "FROM orders o\n" +
                "LEFT JOIN shop s ON o.shop_id = s.shop_id\n" +
                "LEFT JOIN users u ON o.u_id = u.u_id\n" +
                "LEFT JOIN address a ON o.address_id = a.address_id\n" +
                "WHERE o.order_status = ? AND o.order_type = ?;";
        BeanListHandler<OrderDTO> beanListHandler = new BeanListHandler<>(OrderDTO.class);
        return sqlExecutor.executeQuery(sql, beanListHandler, oderState, orderType);
    }



    @Override
    public Rider centerRider(String phone) {
        SqlExecutor sqlExecutor = new SqlExecutor(DBUtils.getConnection());
        String sql = "select rider_name,rider_phone,rider_type from rider where rider_phone = ?";

        BeanHandler<Rider> riderBeanHandler = new BeanHandler<>(Rider.class);

        return sqlExecutor.executeQuery(sql,riderBeanHandler,phone);
    }
}
